package com.source.lightfowl.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class DateUtils {

    public String getYear() {
        Date date = new Date();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        String year = yyyy.format(date);
        return year;
    }

    public String getDate() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        String day = formatDate.format(date);
        return day;
    }

    public String getMiniute() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String minute = formatDate.format(date);
        return minute;
    }

    public int getHm() {
        Date date = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("HHmm");
        String hm = formatDate.format(date);
        Integer integer = Integer.valueOf(hm);
        return integer;
    }

    public String getBeforeDaysDate(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -days);
        Date date = calendar.getTime();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        return formatDate.format(date);
    }

    public Date getDateBeforeDaysDate(Integer days, Date sourceDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sourceDate);
        calendar.add(Calendar.DATE, -days);
        Date date = calendar.getTime();
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        return stringToDate(formatDate.format(date));
    }

    public Date stringToDate(String str) {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = formatDate.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public String stringToYMD(Date date) {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        String str ="";
        str = formatDate.format(date);
        System.out.println(str);
        return str;
    }

}
